using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Dynamic;
using System.Data.SqlClient;
using System.Configuration;
using Model;
using Common;

namespace DAL
{
    public class QuanDAL
    {
        public static int Save(QuanInfo info)
        {
            string sql = string.Format("insert into [quan]([AdminID],[Money],[Man],[IsShow],[BeginDate],[EndDate],[CreateDate]) values(@AdminID,@Money,@Man,@IsShow,@BeginDate,@EndDate,@CreateDate);select @@identity;");
            SqlParameter[] paras = new SqlParameter[]{
                new SqlParameter("@AdminID", info.AdminID),
                new SqlParameter("@Money", info.Money),
                new SqlParameter("@Man", info.Man),
                new SqlParameter("@IsShow", info.IsShow),
                new SqlParameter("@BeginDate", info.BeginDate),
                new SqlParameter("@EndDate", info.EndDate),
                new SqlParameter("@CreateDate", info.CreateDate)
            };
            return DataConvert.ToInt(CommonDAL.ExecuteScalar(sql, paras));
        }
        public static int Update(QuanInfo info)
        {
            string sql = string.Format("update [quan] set [AdminID]=@AdminID,[Money]=@Money,[Man]=@Man,[IsShow]=@IsShow,[BeginDate]=@BeginDate,[EndDate]=@EndDate where [ID]=@ID");
            SqlParameter[] paras = new SqlParameter[]{
                new SqlParameter("@AdminID", info.AdminID),
                new SqlParameter("@Money", info.Money),
                new SqlParameter("@Man", info.Man),
                new SqlParameter("@IsShow", info.IsShow),
                new SqlParameter("@BeginDate", info.BeginDate),
                new SqlParameter("@EndDate", info.EndDate),
                new SqlParameter("@ID", info.ID)
            };
            return CommonDAL.ExecuteNoneQuery(sql, paras) ? info.ID : 0;
        }
        public static QuanInfo FindOneByID(int id)
        {
            DataRow dr = CommonDAL.FindOneByID(id, "[quan]");
            return SimpleData(dr);
        }
        public static List<QuanInfo> FindListByIDs(string ids)
        {
            DataTable dt = CommonDAL.FindListByIDs(ids, "[quan]");
            return ListSimpleData(dt);
        }
        public static List<QuanInfo> FindPage(int adminid, string keyword, string begindate, string enddate, int startindex, int pagesize, string order, string desc, out int count)
        {
            StringBuilder filter = new StringBuilder();
            filter.AppendFormat(" and [quan].AdminID={0}", adminid);
            if (!string.IsNullOrEmpty(keyword))
            {
                filter.AppendFormat(" and [quan].Name like '%{0}%'", keyword);
            }
            if (!string.IsNullOrEmpty(begindate) && !string.IsNullOrEmpty(enddate))
            {
                filter.AppendFormat(" and [quan].CreateDate between '{0}' and '{1}'", DataConvert.ToDateTime(begindate).ToString("yyyy-MM-dd 00:00:00"), DataConvert.ToDateTime(enddate).ToString("yyyy-MM-dd 23:59:59"));
            }
            DataTable dt = CommonDAL.FindPage(startindex, pagesize, order, desc, filter, "[quan]", out count);
            return ListSimpleData(dt);
        }
        public static List<QuanInfo> FindAll(int adminid)
        {
            DataTable dt = CommonDAL.GetDataTable(string.Format("select top 6 * from [quan] where AdminID={0} and IsShow=1 and getdate() between BeginDate and EndDate order by Money asc",adminid));
            return ListSimpleData(dt);
        }
        public static List<QuanInfo> FindTop(int adminid, int top)
        {
            DataTable dt = CommonDAL.GetDataTable(string.Format("select top {0} * from [quan] where AdminID={1} and IsShow=1 and getdate() between BeginDate and EndDate order by Money asc", top, adminid));
            return ListSimpleData(dt);
        }
        private static QuanInfo SimpleData(DataRow dr)
        {
            QuanInfo info = new QuanInfo();
            if (dr != null)
            {
                info.ID = DataConvert.ToInt(dr["ID"]);
                info.AdminID = DataConvert.ToInt(dr["AdminID"]);
                info.Money = DataConvert.ToFloat(dr["Money"]);
                info.Man = DataConvert.ToFloat(dr["Man"]);
                info.IsShow = DataConvert.ToInt(dr["IsShow"]);
                info.BeginDate = DataConvert.ToDateTime(dr["BeginDate"]);
                info.EndDate = DataConvert.ToDateTime(dr["EndDate"]);
                info.CreateDate = DataConvert.ToDateTime(dr["CreateDate"]);
            }
            return info;
        }
        private static QuanInfo AllData(DataRow dr)
        {
            QuanInfo info = new QuanInfo();
            if (dr != null)
            {
                info.ID = DataConvert.ToInt(dr["ID"]);
                info.AdminID = DataConvert.ToInt(dr["AdminID"]);
                info.Money = DataConvert.ToFloat(dr["Money"]);
                info.Man = DataConvert.ToFloat(dr["Man"]);
                info.IsShow = DataConvert.ToInt(dr["IsShow"]);
                info.BeginDate = DataConvert.ToDateTime(dr["BeginDate"]);
                info.EndDate = DataConvert.ToDateTime(dr["EndDate"]);
                info.CreateDate = DataConvert.ToDateTime(dr["CreateDate"]);
            }
            return info;
        }
        private static List<QuanInfo> ListSimpleData(DataTable dt)
        {
            List<QuanInfo> list = new List<QuanInfo>();
            foreach (DataRow row in dt.Rows)
            {
                list.Add(SimpleData(row));
            }
            return list;
        }
        private static List<QuanInfo> ListAllData(DataTable dt)
        {
            List<QuanInfo> list = new List<QuanInfo>();
            foreach (DataRow row in dt.Rows)
            {
                list.Add(AllData(row));
            }
            return list;
        }
    }
}

